package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.base.utils.ExcelUtil.ExcelLogs;
import com.qy.base.utils.ExcelUtil.ExcelUtil;
import com.qy.base.utils.UploadFile;
import com.qy.model.*;
import com.qy.service.*;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-ChargeController")
@RequestMapping("/manage/charge")
public class ChargeController {
    @Resource
    private ChargeService chargeService;

    @Resource
    private TollService tollService;

    @Resource
    private PolicyOrderService policyOrderService;

    @Resource
    private EnshrineService enshrineService;

    @Resource
    private MemberService memberService;

    //政策库-负责人列表-新增
    @PostMapping("/add")
    public Result add(@RequestBody Charge charge) {
        if (charge.getCharge_name() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getUnit_name() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getBusiness_scope() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getOffice_tel() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getPhone() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        charge.setPublisher("平台");
        charge.setAdd_time(String.valueOf(new Date().getTime()));
        chargeService.save(charge);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        chargeService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Charge charge) {
        chargeService.update(charge);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Charge charge = chargeService.findById(id);
        return ResultGenerator.successResult(charge);
    }

    @GetMapping("/list")
    public Result list(PageBean<Charge> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Charge> list = chargeService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //政策库-负责人-点击编辑
    @GetMapping("/seeCharge")
    public Result seeCharge(Integer id) {
        if (id == null || id == 0) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Charge> list = chargeService.seeCharge(id);
        return ResultGenerator.successResult(list.get(0));
    }

    //政策库-负责人列表
    @GetMapping("/chargeList")
    public Result chargeList(PageBean<Map> page, Integer type, String content, Charge charge) {
        if (type == null || type == 0) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        } else if (type == 1) {
            charge.setCharge_name(content);
        } else if (type == 2) {
            charge.setUnit_name(content);
        } else if (type == 3) {
            charge.setOffice_tel(content);
        } else if (type == 4) {
            charge.setPublisher(content);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Map> list = chargeService.chargeList(charge);
        page.setList(list);
//        if (list.size() == 0){
//            return ResultGenerator.errResult(1,"查询结果不存在");
//        }
        return ResultGenerator.successResult(page);
    }

    //政策库-负责人列表-编辑提交
    @PostMapping("updateCharge")
    public Result updateCharge(@RequestBody Charge charge) {
        if (charge.getId() == null || charge.getId() == 0) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        if (charge.getCharge_name() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getUnit_name() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getBusiness_scope() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (charge.getOffice_tel() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        chargeService.updateCharge(charge);
        return ResultGenerator.successResult();
    }

    //政策库-负责人列表-删除
    @PostMapping("/deleteCharge")
    public Result deleteCharge(@RequestBody Charge charge) {
        if (charge.getId() == null || charge.getId() == 0) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        chargeService.deleteCharge(charge);
        return ResultGenerator.successResult();
    }

    //政策库-负责人列表-上下架
    @PostMapping("/chareState")
    public Result chareState(@RequestBody Charge charge) {
        if (charge.getId() == null || charge.getId() == 0) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        chargeService.chargeState(charge);
        return ResultGenerator.successResult();
    }

    //导入
    @PostMapping("/Import")
    public Result Import(@RequestBody Charge charge) throws Exception {
        String fileUrl = null;
        if (charge.getExcel() != null && !charge.getExcel().isEmpty()) {
            fileUrl = UploadFile.uploadBase64(charge.getExcel());
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }

        File f = new File("../" + fileUrl);
        InputStream inputStream = new FileInputStream(f);

        ExcelLogs logs = new ExcelLogs();
        Collection<Map> importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs, 0);

        if (importExcel.size() != 0) {
            for (Map m : importExcel) {
                Charge charge1 = new Charge();
                charge1.setCharge_name(m.get("负责人姓名").toString().replaceAll(" ",""));
                charge1.setUnit_name(m.get("所在单位").toString());
                if (m.get("职务内容（原业务范围修改为职务内容）") != null && !m.get("职务内容（原业务范围修改为职务内容）").toString().isEmpty()) {
                    charge1.setBusiness_scope(m.get("职务内容（原业务范围修改为职务内容）").toString());
                }
                if (m.get("办公室电话") != null && !m.get("办公室电话").toString().isEmpty()) {
                    charge1.setOffice_tel(m.get("办公室电话").toString());
                }
                if (m.get("手机号（非必填）") != null && !m.get("手机号（非必填）").toString().isEmpty()) {
                    charge1.setPhone(m.get("手机号（非必填）").toString());
                }
                charge1.setHour(m.get("上班时间（时）").toString());
                charge1.setMinute(m.get("上班时间（分）").toString());
                charge1.setSecond(m.get("上班时间（秒）").toString());
                charge1.setPublisher("平台");
                charge1.setAdd_time(DateUtil.getNowTimestamp());
                chargeService.save(charge1);
            }
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        return ResultGenerator.successResult();
    }

    //政策-政策新增-获取全部负责人
    @GetMapping("/selectChargeList")
    public Result selectChargeList() {
        List<Charge> list = chargeService.selectChargeList();
        return ResultGenerator.successResult(list);
    }

    //政策-政策新增-获取全部负责人-模糊搜索
    @GetMapping("/selectVagueChargeList")
    public Result selectVagueChargeList(String unit_name, PageBean<Map> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Map> list = chargeService.selectVagueChargeList(unit_name);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

}
